#include "RingQueue.hpp"
#include"Task.hpp"
#include <pthread.h>
#include <time.h>
#include <unistd.h>

using namespace Mango;

void* consumer(void* args)
{
    RingQueue<Task>* rq = (RingQueue<Task>*)args;
     while(true)
     {
         Task t;
         rq->Pop(&t);
         t(); //相当于t.operator()  ->t.Run()
         sleep(1);
     }
}
void* producter(void* args)
{
    RingQueue<Task>* rq = (RingQueue<Task>*)args;
    const std::string ops = "+-*/%";
    while(true)
    {
        sleep(1);
        int x = rand()%20 + 1;
        int y = rand()%10 + 1;
        char op = ops[rand()%ops.size()];
        Task t(x, y, op);
        std::cout << "生产数据是:  " << t.Show() << "当前线程是: " << pthread_self()<< std::endl;
        rq->Push(t);
    }
}
int main()
{
    //改成多生产者多消费者模型
    srand((long long)time(nullptr));
    RingQueue<Task>* rq = new RingQueue<Task>();

    pthread_t c0,c1,c2,c3,p0,p1,p2;

    pthread_create(&c0, nullptr, consumer, (void*)rq);
    pthread_create(&c1, nullptr, consumer, (void*)rq);
    pthread_create(&c2, nullptr, consumer, (void*)rq);
    pthread_create(&c3, nullptr, consumer, (void*)rq);
    pthread_create(&p0, nullptr, producter, (void*)rq);
    pthread_create(&p1, nullptr, producter, (void*)rq);
    pthread_create(&p2, nullptr, producter, (void*)rq);

    pthread_join(c0, nullptr);
    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(c3, nullptr);
    pthread_join(p0, nullptr);
    pthread_join(p1, nullptr);
    pthread_join(p2, nullptr);
    return 0;
}
// int main()
// {
//     //单生产单消费模型
//     srand((long long)time(0));
    
//     RingQueue<int>* rq = new RingQueue<int>();

//     pthread_t c,p;
//     //把环形队列以参数的形式传给两个线程,相当于两个线程有了进行通信的数据区域
//     pthread_create(&c, nullptr, consumer, (void*)rq);
//     pthread_create(&p, nullptr, producter, (void*)rq);

//     pthread_join(c, nullptr);
//     pthread_join(p, nullptr);
//     return 0;
// }
